<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类名称" prop="category_name">
        <el-input
          v-model="queryParams.category_name"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="handleQuery">查询</el-button>
        <el-button type="primary" size="mini" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >批量删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="catList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分类编号" align="center" prop="display_order" />
      <el-table-column label="分类名称" align="center" prop="category_name" />
      <el-table-column label="分类描述" align="center" prop="description" />
      <el-table-column label="备注" align="center" prop="remark"/>
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>-->
      <!--        </template>-->
      <el-table-column label="创建时间" align="center" prop="create_time" width="180"/>
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.createTime) }}</span>-->
      <!--        </template>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="Delete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--    //新增按钮的对话框  分类名称  分类描述  备注 提交 取消-->
    <el-dialog title="新增分类" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="category_name">
          <el-input v-model="form.category_name" placeholder="请输入分类名称"  />
        </el-form-item>
        <el-form-item label="分类描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入分类描述" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>


import {deleteCat, listCat, updateCat} from "@/api/cf/cat";
import {addCat} from "@/api/cf/cat";


export default {
  name: "Cat",
  data() {
    return {
      // 分页
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        category_name: undefined,
        category_type: undefined,


      },
      //新增分类的表单数据
      form: {
        category_name: '',
        description: '',
        remark: '',
      },
      //新增分类的表单验证规则
      rules: {
        category_name: [
          {required: true, message: '请输入分类名称', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入分类描述', trigger: 'blur'}
        ],
      },
      //标题
      title: '',
      open: false,
      //遮罩层
      loading: true,
      showSearch: true,
      single: true,
      multiple: true,
      // 分类列表
      catList: [],
      // 总条数
      total: 0,
      //复选框
      ids: [],
    }
  },
  // 分类列表 页面加载完成
  created() {
    this.getList()
  },
  methods: {
    // 查询分类列表
    getList() {
      listCat(this.queryParams).then(res => {
        this.catList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    //表单重置
    handleReset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        category_name: undefined,
        category_type: undefined,
      }
      this.getList()
    },


    // 查询按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },



    // 新增按钮
    handleAdd() {
      this.title = '新增分类'
      this.open = true
      this.form = {
        category_name: '',
        description: '',
        remark: '',
      }
    },
    //修改按钮发请求给后端
    // 修改按钮（假设通过 scope.row 获取选中行数据）
    handleUpdate(scopeRow) {
      this.title = '修改分类';
      this.open = true;
      // 关键：将选中行的 id 绑定到表单
      this.form = {
        id: scopeRow.id, // 传递 id
        category_name: scopeRow.category_name,
        description: scopeRow.description,
        remark: scopeRow.remark
      };
    },
    //批量删除
    handleDelete() {
      if (this.ids.length === 0) {
        this.$message.warning("请选择需要删除的分类");
        return;
      }
      this.$confirm("确定删除选中分类吗？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        // 调用后端删除接口，而非路由跳转
        deleteCat(this.ids).then(res => {
          if (res.code === 200) {
            this.$message.success("删除成功");
            this.getList(); // 刷新列表
          }
        });
      });
    },
    // 删除按钮
    Delete(row) {
      this.$confirm("确定删除选中分类吗？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        // 调用后端删除接口，而非路由跳转
        deleteCat([row.id]).then(res => {
          if (res.code === 200) {
            this.$message.success("删除成功");
            this.getList(); // 刷新列表
          }
        });
      });
    },
    // 复选框
    handleSelectionChange(val) {
      this.ids = val.map(item => item.id) // 选中的分类id
      this.single = val.length !== 1 // 只能选择一条
      this.multiple = !val.length // 只能选择一条
    },
    // 分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    },
    //提交表单
    submitForm() {
      // 新增分类
      if (this.title === '新增分类') {
        this.$refs.form.validate().then(valid => {
          if (valid) {
            // 新增分类
            addCat(this.form).then(res => {
              console.log(res)
              if (res.code === 200) {
                this.$message({
                  message: '新增成功',
                  type: 'success'
                })
                this.open = false
                this.getList()
              }
            })
          }
        })
      }
      // 修改分类
      if (this.title === '修改分类') {
        this.$refs.form.validate().then(valid => {
          if (valid) {
            // 修改分类
            updateCat(this.form).then(res => {
              console.log(res)
              if (res.code === 200) {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
                this.open = false
                this.getList()
              }
            })
          }
        })
      }
    },
    //取消
    cancel() {
      this.open = false
    },
  }
}
</script>

<style scoped lang="scss">

</style>
